// 分类模块
import { findHeadCategory } from '@/api/home.js'
import { category } from '@/utils/constant.js'
export default {
  namespaced: true,
  state: {
    list: category.map((item, i) => ({ id: i, name: item }))
  },
  mutations: {
    // 修改list的数据
    setList (state, val) {
      state.list = val
    },

    // 修改标志位,显示
    show (state, id) {
      const category = state.list.find(item => item.id === id)
      if (category) {
        category.open = true
      }
    },

    // 修改标志位,隐藏
    hide (state, id) {
      const category = state.list.find(item => item.id === id)
      if (category) {
        category.open = false
      }
    }

  },
  actions: {
    async getList (ctx) {
      try {
        // 调用接口获取分类数据
        const res = await findHeadCategory()

        // 添加标志位
        res.result.forEach(item => { item.open = false })

        // 存入Vuex
        ctx.commit('setList', res.result)
      } catch {
        alert('获取分类数据失败')
      }
    }
  },
  getters: {}
}
